<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <!-- <el-form-item label="测评id" prop="scaleId">
        <el-input
          v-model="queryParams.scaleId"
          placeholder="请输入测评id"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item label="量表名称:" prop="scaleTitleShow" label-width="5rem">
        <el-input v-model="queryParams.scaleTitleShow" placeholder="请输入量表名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <!-- <el-form-item label="登录名" prop="loginName">
        <el-input
          v-model="queryParams.loginName"
          placeholder="请输入登录名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="部门" prop="department">
        <el-input
          v-model="queryParams.department"
          placeholder="请输入部门"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="分配时间" prop="allocateTime">
        <el-input
          v-model="queryParams.allocateTime"
          placeholder="请输入分配时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否设置作答时间" prop="answerTime">
        <el-input
          v-model="queryParams.answerTime"
          placeholder="请输入是否设置作答时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="作答开始时间" prop="startTime">
        <el-input
          v-model="queryParams.startTime"
          placeholder="请输入作答开始时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="作答截止时间" prop="endTime">
        <el-input
          v-model="queryParams.endTime"
          placeholder="请输入作答截止时间"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="重测" prop="retest">
        <el-input
          v-model="queryParams.retest"
          placeholder="请输入重测"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="重测时间间隔" prop="retestInterval">
        <el-input
          v-model="queryParams.retestInterval"
          placeholder="请输入重测时间间隔"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否发送通知" prop="notification">
        <el-input
          v-model="queryParams.notification"
          placeholder="请输入是否发送通知"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <!-- <el-form-item label="是否可查看报告" prop="report">
        <el-input
          v-model="queryParams.report"
          placeholder="请输入是否可查看报告"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item> -->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <!-- <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd"
          v-hasPermi="['system:myTest:add']">新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single" @click="handleUpdate"
          v-hasPermi="['system:myTest:edit']">修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple" @click="handleDelete"
          v-hasPermi="['system:myTest:remove']">删除</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport"
          v-hasPermi="['system:myTest:export']">导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
    <!-- <router-link :to="{ path: '/mentalTest/zhiqingquan' }">
      <el-button size="mini" type="text" icon="el-icon-edit">作答</el-button>
    </router-link> -->


    <el-table v-loading="loading" :data="myTestList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="序号" align="center" prop="id" /> -->
      <!-- <el-table-column label="测评id" align="center" prop="scaleId" /> -->
      <el-table-column label="量表名称" align="center" prop="scaleTitleShow" />
      <!-- <el-table-column label="登录名" align="center" prop="loginName" /> -->
      <!-- <el-table-column label="姓名" align="center" prop="userName" /> -->
      <!-- <el-table-column label="部门" align="center" prop="department" /> -->
      <!-- <el-table-column label="分配时间" align="center" prop="allocateTime" /> -->
      <!-- <el-table-column label="是否设置作答时间" align="center" prop="answerTime" /> -->
      <!-- <el-table-column label="作答开始时间" align="center" prop="startTime" /> -->
      <el-table-column label="结束时间" align="center" prop="endTime" />
      <!-- <el-table-column label="作答状态" align="center" prop="answeringStatus" /> -->
      <!-- <el-table-column label="测试类型" align="center" prop="testType" /> -->
      <!-- <el-table-column label="重测" align="center" prop="retest" /> -->
      <!-- <el-table-column label="重测时间间隔" align="center" prop="retestInterval" /> -->
      <!-- <el-table-column label="是否发送通知" align="center" prop="notification" /> -->
      <!-- <el-table-column label="通知类型" align="center" prop="notificationType" /> -->
      <!-- <el-table-column label="是否可查看报告" align="center" prop="report" /> -->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" prop="answeringStatus">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="gotoKnow(scope.row)"
            :disabled="scope.row.answeringStatus === '已作答'"
            :class="{'red-button': scope.row.answeringStatus === '已作答', 'disabled-button': scope.row.answeringStatus === '已作答'}">
            <!-- {{scope.row.answeringStatus}} -->
          </el-button>
          <!-- <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['system:myTest:remove']"
          >删除</el-button> -->
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import { listMyTest, getMyTest, delMyTest, addMyTest, updateMyTest } from "@/api/mentalTest/myTest";

export default {
  name: "MyTest",
  data () {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 我的测验表格数据
      myTestList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        scaleId: null,
        scaleTitle: null,
        scaleTitleShow:null,
        loginName: null,
        userName: null,
        department: null,
        allocateTime: null,
        answerTime: null,
        startTime: null,
        endTime: null,
        answeringStatus: null,
        testType: null,
        retest: null,
        retestInterval: null,
        notification: null,
        notificationType: null,
        report: null,
        riskLevel:10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created () {
    this.getList();
  },
  // 使用 beforeRouteEnter 导航守卫
  beforeRouteEnter(to, from, next) {
    next(vm => {
      vm.getList();
    });
  },
  methods: {
    /** 查询我的测验列表 */
    getList () {
      this.loading = true;
      listMyTest(this.queryParams).then(response => {
        this.myTestList = response.rows;
        console.log("myTest数据",response.rows);
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel () {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset () {
      this.form = {
        id: null,
        scaleId: null,
        scaleTitle: null,
        scaleTitleShow:null,
        loginName: null,
        userName: null,
        department: null,
        allocateTime: null,
        answerTime: null,
        startTime: null,
        endTime: null,
        answeringStatus: null,
        testType: null,
        retest: null,
        retestInterval: null,
        notification: null,
        notificationType: null,
        report: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery () {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery () {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange (selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd () {
      this.reset();
      this.open = true;
      this.title = "添加我的测验";
    },
    /** 修改按钮操作 */
    handleUpdate (row) {
      this.reset();
      const id = row.id || this.ids
      getMyTest(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改我的测验";
      });
    },
    //跳到知情同意书
    gotoKnow (row) {
      this.$router.push({
        path: '/mentalTest/zhiqingquan',
        query: {
          scaleId: row.scaleId,
          id:row.id
        }
      })
    },
    /** 提交按钮 */
    submitForm () {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateMyTest(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addMyTest(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete (row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除我的测验编号为"' + ids + '"的数据项？').then(function () {
        return delMyTest(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport () {
      this.download('system/myTest/export', {
        ...this.queryParams
      }, `myTest_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
<style>
.red-button {
  background-color: red;
  color: white;
}

.disabled-button {
  pointer-events: none;
  opacity: 0.5;
}
</style>
